<template>
  <div class="app-container">
    <div class="top-bar-container">
      <el-row>
        <el-col :span="16">
          <div class="search-container">
            <search-form @search-submit="getOperationLogList">
              <search-input v-model="listQuery.sysUserId" placeholder="系统用户ID" />
              <search-input v-model="listQuery.url" placeholder="访问地址" />
              <search-input v-model="listQuery.params" placeholder="请求参数" />
            </search-form>
          </div>
        </el-col>
      </el-row>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit stripe highlight-current-row>
      <el-table-column align="center" label="ID">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="系统用户ID">
        <template slot-scope="{row}">
          <span>{{ row.sysUserId }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="用户名">
        <template slot-scope="{row}">
          <span>{{ row.username }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="访问地址">
        <template slot-scope="{row}">
          <span>{{ row.url }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="访问方式">
        <template slot-scope="{row}">
          <span>{{ row.method }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="请求参数">
        <template slot-scope="{row}">
          <span>{{ row.params }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="创建时间">
        <template slot-scope="{row}">
          <span>{{ row.createdTime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getOperationLogList" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination/index'
import SearchForm from '@/components/SearchForm/index'
import SearchInput from '@/components/SearchForm/SearchInput/index'
import { operationLogList } from '@/api/rest/log'

export default {
  name: 'OperatipnLogList',
  components: { SearchInput, Pagination, SearchForm },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        id: null,
        url: null,
        sysUserId: null,
        params: null,
        page: 1,
        limit: 10
      },
      ancestors: null,
      dialogEditVisible: false
    }
  },
  watch: {
    $route(to, from) {
      this.getOperationLogList()
    }
  },
  created() {
    this.getOperationLogList()
  },
  methods: {
    getOperationLogList() {
      this.listLoading = true
      const sysUserId = this.listQuery.sysUserId
      if (!this.checkNaN(sysUserId)) {
        this.$message.error('系统用户ID格式不正确')
        this.listLoading = false
      } else {
        operationLogList(this.listQuery).then(response => {
          this.list = response.data.items
          this.total = response.data.total
          this.listLoading = false
        })
      }
    },
    handleEditFormDialogClose() {
      this.$refs.editForm.clearValidate()
    },
    checkNaN(value) {
      if (value === '' || value == null) {
        return true
      }
      value = value * 1
      return typeof value === 'number' && !isNaN(value)
    }
  }
}
</script>
<style scoped>
</style>

